package com.pompom.workers;

import gummy.database.GummyClient;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import nl.ansuz.android.steam.dota.request.DotaRequest;
import nl.ansuz.android.steam.dota.response.match.GetMatchHistoryResponse;
import nl.ansuz.android.steam.request.SteamRequest;
import nl.ansuz.android.steam.request.user.GetPlayerSummaries;
import nl.ansuz.android.steam.response.user.GetPlayerSummariesResponse;
import nl.ansuz.android.steam.util.GsonUtil;
import nl.ansuz.android.steam.vo.user.UserVO;

import com.google.gson.Gson;
import com.pompom.matchcrawler.ManipulaHttp2;
import com.pompom.matchcrawler.MatchHistoryFetcher;
import com.pompom.matchcrawler.types.TOAccountWorkQueue;
import com.pompom.matchcrawler.types.TOMatchErrorQueue;
import com.pompom.matchcrawler.types.TOMatchSkillInfo;
import com.pompom.matchcrawler.types.TOMatchWorkQueue;
import com.pompom.matchcrawler.types.TOPlayerInfo;

public class ErrorHandlerWorker extends AbstractWorker{


 
	private TOMatchErrorQueue[] queue=null; 
	private int fetchSize=10;

	
	private void retrieveNextErrorsFromQueue()
	{
		queue = (TOMatchErrorQueue[]) GummyClient.getInstance("QUEUES").executeQueryTO
				("SELECT * FROM MATCH_ERROR_QUEUE LIMIT "+fetchSize, new Object[]{}, TOMatchErrorQueue.class);

		 
	}
	
	public ErrorHandlerWorker()
	{
	 
	}




	public void work()
	{


	 
			retrieveNextErrorsFromQueue();
	 

		if ( queue !=null && queue.length>0)
		{ 
			 for (int i=0;i<queue.length;i++)
			 {
					long matchid = queue[i].getMatchid();
														 					 
					cleanUpMatch(matchid);
					MatchDetailWorker.putMatchInWorkQueue(matchid, TOMatchWorkQueue.PRIORITY_DONTBUFFER);
					clearMatchFromErrorQueue(matchid);
					
			 } 
		sleepPlus=0;	    
			 
		}
		else
		{
			
			if (sleepPlus==0)
				System.out.println("MATCH_ERROR_QUEUE Queue is empty!");
			sleepPlus = 1000;
		}


	}

 


	@Override
	public void setParameters() { 

		 if (getParameter("fetchSize")!=null)
		 {
			 fetchSize = Integer.parseInt(getParameter("fetchSize"));
		 }
	}

	
	 

	
	private void cleanUpMatch(long matchid)
	{
		
		GummyClient.getInstance("DT2").execute("DELETE FROM INVALID_MATCH where MATCHID= ?", new Object[]{""+matchid});
		GummyClient.getInstance("DT2").execute("DELETE FROM MATCHES where MATCHID= ?", new Object[]{""+matchid});
		GummyClient.getInstance("DT2").execute("DELETE FROM PLAYER where MATCHID= ?", new Object[]{""+matchid});
		GummyClient.getInstance("DT2").execute("DELETE FROM INVENTORY where MATCHID= ?", new Object[]{""+matchid});
		GummyClient.getInstance("DT2").execute("DELETE FROM MATCHUNITNAME where MATCHID= ?", new Object[]{""+matchid});
		GummyClient.getInstance("DT2").execute("DELETE FROM MATCH_STATS where MATCHID= ?", new Object[]{""+matchid});
		GummyClient.getInstance("DT2").execute("DELETE FROM SKILLBUILDNEW where MATCHID= ?", new Object[]{""+matchid}); 
		GummyClient.getInstance("DT2").execute("DELETE FROM MATCH_TEAM_INFO where MATCHID= ?", new Object[]{""+matchid});
	}
	 
	 

	 
	 private void clearMatchFromErrorQueue(long matchId)
	 {
			GummyClient.getInstance("QUEUES").execute("DELETE FROM MATCH_ERROR_QUEUE where MATCHID= ?", new Object[]{""+matchId});
			//System.out.println("DELETE FROM ACCOUNT_WORK_QUEUE where ACCOUNTID= "+accountId);
	 }


}
